'use strict';

const BaseController = require('./BaseController');
class LoginController extends BaseController {
  async index(){
    const { ctx, app } = this;
    const query = ctx.request.query;
    query.model = 'User';
    const res = await ctx.service.page.page(query)
    ctx.body = res
  }
  async create() {
    const { ctx, app } = this;
    let user = {};
    user.username = ctx.request.body.id;
    user.password = ctx.request.body.password;
    if(user.username && user.password){
      try{
        const result = await ctx.model.User.findOne({
          where: user
        })
        if(result){
          result.role_authority = eval(result.role_authority)
          this.setUserInfo(result);
          ctx.body = {code:1,msg:"登录成功",data:result}
        }else{
          ctx.body = {code:0,msg:"用户名或密码错误"}
        }
      }catch(e){
        ctx.body = {code:0,msg:e}
      }
    }else{
      ctx.body = {code:0,msg:"请将信息填写完整！"}  
    }
  }
  async update(){
    const { ctx, app } = this;
    if(ctx.params.id && Object.keys(ctx.request.body).length>0){
      await ctx.model.User.update(ctx.request.body,{where:{id:ctx.params.id}})
      ctx.body = {code:1,msg:"修改成功"}
    }else{
      ctx.body = {code:0,msg:"无数据修改！"}
    }
  }
}

module.exports = LoginController;
